import { products, categories, brands } from "@supersavely/db/schema";
import db from "@/lib/db"; // 使用 drizzle 数据库提供者
import { eq, like } from "drizzle-orm";
import axios from "axios";
import Database from "@/lib/db";
import { users } from "@supersavely/db/schema";
import { NextApiRequest, NextApiResponse } from "next";

export async function POST(req: any, res: any) {
  try {
    // 从请求体中解析出 JSON 数据
    const {
      name,
      brandId,
      categoryId,
      minPrice,
      maxPrice,
      pageNum = 1,
      pageSize = 10,
    } = await req.json();
    // 计算偏移量
    const offset = (pageNum - 1) * pageSize;
    // 初始化查询条件
    let query: any = Database.database
      .select()
      .from(products)
      .limit(pageSize)
      .offset(offset);
    // 设置偏移量;
    // 根据提供的参数添加查询条件
    if (name) {
      query = query.where(like(products.name, `%${name}%`));
    }
    if (brandId) {
      query = query.where(eq(products.brandId, brandId));
    }
    if (categoryId) {
      query = query.where(eq(products.categoryId, categoryId));
    }
    const searchResults = await query;
    return Response.json({ data: searchResults, total: 100 });
  } catch (error: any) {
    return Response.json({ data: 500 });
  }
}

// export async function getProdcutlist(query: any) {
//   let arr = await axios
//     .get("http://localhost:3000/api/poduct", { ...query })
//     .then((res) => {
//       console.log("34345", res.data);
//       return res.data;
//     });
//   return arr;
// }
// export async function getList(query: any) {
//   // const allUsers = await Database.database.select().from(users);
//   // console.log("这个aaa", allUsers);
//   let arr = await axios
//     .get("http://localhost:3000/api/demo", { ...query })
//     .then((res) => {
//       console.log("34345", res.data);
//       return res.data;
//     });
//   return arr;
// }
